<?php
	require(I_PATH . '/regExp.php');
	$bd = openBD();
	if(isset($_POST["nom"])){
		if (validateMail($_POST["mail"]) && validateL($_POST["nom"]) && validateL($_POST["prenom"])){
			try{
				$stm = $bd->prepare("UPDATE CLIENT SET nom=:nom, prenom=:prenom, mail=:mail, account = :account + account WHERE login = :login");
				$stm->bindParam(':login', $_SESSION["login"]);
				$stm->bindParam(':nom', $_POST["nom"]);
				$stm->bindParam(':prenom', $_POST["prenom"]);
				$stm->bindParam(':mail', $_POST["mail"]);
				$stm->bindParam(':account', $_POST["newAccount"]);
				$stm->execute();
			}
			catch (PDOException $err) {
				echo "Error " . $err->getMessage();
			}
			echo "<p class=\"ui-state-highlight\">Votre profil à été mis à jour.</p> "; 
		}
		else{
			echo "<p class=\"ui-state-error\">Vérifiez les données saisies.</p> "; 
		}
	}
	$stm = $bd->prepare("SELECT * FROM CLIENT WHERE login = :login");
	$stm->bindParam(':login', $_SESSION["login"]);
	$stm->execute();
	$row = $stm->fetch(PDO::FETCH_ASSOC);
?>

<script>
$(function() {
	$( "#currency" ).change(function() {
		$( "#spinner" ).spinner({ culture: "en-US" });
	});

	$( "#spinner" ).spinner({
		min: 1,
		max: 500,
		step: 5,
		start: 1000,
		numberFormat: "C"
	});
	
	$( "#dialog-charger" ).dialog({
		autoOpen: false,
		resizable: false,
		height:200,
		width:500,
		modal: true,
		buttons: {
			"Charger votre compte": function() {
				$("#montantACharger").contents().filter(function() {
					return this.nodeType == 3}).replaceWith($("#spinner").val() + " €");
				$("#montantACharger" ).css("visibility", "visible");
				$("#newAccount" ).val($("#spinner").val());
				$( this ).dialog( "close" );
			},
			"Annuler": function() {
				$( this ).dialog( "close" );
			}
		}
	});
	
	$( "#btnChargerCompte" )
		.click(function() {
			$( "#dialog-charger" ).dialog( "open" );
	});
});
</script>

<h1>Editez votre profil </h1>
<form method="post" action="?p=profile">
	<label for="nom">
		Nom :
	</label>
	<input id="nom" name="nom" type="text" value="<?php echo $row["nom"]; ?>"/><br/>
	<label for="prenom">
		Prénom :
	</label>
	<input id="prenom" name="prenom" type="text" value="<?php echo $row["prenom"]; ?>"/><br/>
	<label for="mail">
		E-mail :
	</label>
	<input id="mail" name="mail" type="text" value="<?php echo $row["mail"]; ?>"/><br/>

	<label for="account">
		Montant actuel :
	</label>
	<span>
		<input id="account" name="account" type="text" readonly="readonly" value="<?php echo $row["account"]; ?> €"/>
		<span id="btnChargerCompte" class="ui-icon ui-icon-plusthick" style="position:relative; bottom:20px;left:470px" 
			title="Cliquez pour recharger votre compte"></span>
		<span id="montantACharger" style="position:relative; bottom:38px;left:190px;visibility:hidden">test</span>
	</span>
	<br/>
	<input id="newAccount" name="newAccount" value="0" style="display:none;"/>
	<input type="submit" value="Enregistrer les modifications"/>
	<input type="reset" value="Annuler">
</form>

<div id="dialog-charger" title="Chargez votre compte">
	<label for="spinner">Montant :</label>
	<input id="spinner" name="spinner" value="5" />
</div>